<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-card">
        <div class="register-header">
          <h2>注册</h2>
        </div>
        
        <el-form :model="form" :rules="rules" ref="formRef" class="register-form">
          <el-form-item prop="username">
            <div class="form-label">用户名</div>
            <el-input 
              v-model="form.username" 
              placeholder="" 
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="email">
            <div class="form-label">邮箱（可选）</div>
            <el-input 
              v-model="form.email" 
              placeholder="" 
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <div class="form-label">密码</div>
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="" 
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <div class="form-label">确认密码</div>
            <el-input 
              v-model="form.confirmPassword" 
              type="password" 
              placeholder="" 
              size="large"
              show-password
              @keyup.enter="handleRegister"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              class="register-button" 
              size="large" 
              :loading="loading"
              @click="handleRegister"
            >
              注册
            </el-button>
          </el-form-item>
          
          <div class="back-to-login">
            <span>已有账号？</span>
            <span class="login-link" @click="goToLogin">立即登录</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authAPI } from '@/api'

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== form.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度至少3位', trigger: 'blur' }
  ],
  email: [
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      const response = await authAPI.register({
        username: form.username,
        email: form.email,
        password: form.password
      })
      
      // 保存token和用户信息
      localStorage.setItem('access_token', response.data.tokens.access)
      localStorage.setItem('refresh_token', response.data.tokens.refresh)
      localStorage.setItem('user_info', JSON.stringify(response.data.user))
      
      ElMessage.success('注册成功')
      router.push('/')
    } catch (error) {
      const errorMsg = error.response?.data?.error || '注册失败'
      ElMessage.error(errorMsg)
    } finally {
      loading.value = false
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4a90e2 0%, #50c9c3 50%, #b8e994 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 50%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

.register-content {
  position: relative;
  z-index: 1;
}

.register-card {
  width: 420px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 50px 40px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.register-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.register-form {
  margin-top: 30px;
}

.form-label {
  color: #666;
  font-size: 14px;
  margin-bottom: 8px;
}

.register-button {
  width: 180px;
  background: #4a90e2;
  border: none;
  border-radius: 25px;
  color: white;
  font-size: 15px;
  font-weight: 500;
  height: 45px;
  margin: 0 auto;
  display: block;
}

.register-button:hover {
  background: #357abd;
}

.back-to-login {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 13px;
}

.login-link {
  color: #4a90e2;
  cursor: pointer;
  margin-left: 5px;
}

.login-link:hover {
  color: #357abd;
  text-decoration: underline;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  background: #f8f9fa;
  box-shadow: none;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px 15px;
}

:deep(.el-input__wrapper:hover) {
  border-color: #4a90e2;
}

:deep(.el-input__wrapper.is-focus) {
  background: #fff;
  border-color: #4a90e2;
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.1);
}

:deep(.el-input__inner) {
  color: #333;
}
</style>
